<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

Class BuyController Extends BaseController {

    public function index($id = 0) {

    	$db = $this->registry->db; 
  		$bookDetailContent = $this->registry->template->getBookDetailContent($db->get('bookManager')->getById($id), $db->get('bookManager')->getRelatedBooks($id));
        $book = $db->get('bookManager')->getById($id); 
        $headContent = '                  
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta property="og:title" content="{bookName}" />
            <meta property="og:type" content="book" />
            <meta property="og:url" content="{statisticURL}detail/{id}" />
            <meta property="og:image"
            	content="{statisticURL}views/images/thumbnail/{bookThumbnail}" />
            <meta property="og:site_name" content="Book Store" />
            <meta property="og:description" content="{bookContent}" />
            <meta property="fb:admins" content="1416293132" /><!-- Vinh -->';
            
        $title = $db->get('bookManager')->getById($id)->get('title');
        $tags = array('bstoHead'=> $headContent,
        			 'bid' => $id,
                     'bookName'    	=> $book->get("title"),
			         'bookThumbnail'=> $book->get("thumbnail"),
				 	 'id'			=> $book->get("nid"),
                     'bookContent'  => $book->get("summary"),
        			 'price'        => $book->get('price'));
    	$user = $this->registry->db->get('userManager')->getCurrentUser();
    	
        if($user != null){
        	$tags['login'] = '<li id="login"><span> Hello, '. $user->get('username').'</span><a href="{bstoRootPath}logout">Logout</a></li>';	
        }else{
        	$tags['login'] = '<li id="login"><a href="{bstoRootPath}login">Login</a></li>';
        }   
        $content = $this->bookDeal($tags);
        if($user == null)
        	$content .= $this->login($id);
        else
        	$content .= $this->viewInfor();
        
        $output = $this->registry->template->getPageContent('index', $title, $content, $tags);               
        echo $output;      
    }
    public function bookDeal($tags = array()){
    	$res = $this->registry->template->getTemplate('bookDeal', $tags, 'Module');
    	return $res;
    }
    public function login($id = 0){
    	$method = $_SERVER['REQUEST_METHOD'];
		$params['username'] = "";
		$params['password']= "";	
			
		if ($method=='POST'){
			$username = $_POST['username'];
			$pass = $_POST['password'];
			if (isset($_POST['rememberme'])) {
				$rememberme = $_POST['rememberme'];
			}

			$usermana = $this->registry->db->get('userManager');

			try {
				$usermana->get('username', $username);
				$userlogin = $usermana->authenticate($username, $pass);
				if ($userlogin == null || $userlogin->get('status') == 0) {
					$params['password'] = "Password isn't correct.";
				}
				else {
					$usermana->login($userlogin);
					//Cookie va Session
					if (isset($rememberme)) {
						if ($rememberme == "on") {
							setcookie('userid',$userlogin->get('uid'),time()+3600);
						}
					}
					header("Location: ./$id");
					exit();
				}
			}
			catch (Exception $error) {
				$params['username'] = "Username isn't correct.<br>";
			};
		}
		
		return $this->registry->template->getTemplate('regisLogin', $params, 'Module');
		
    }
	public function viewInfor($id = 0){
		$method = $_SERVER['REQUEST_METHOD'];
		$user = $this->registry->db->get('userManager')->getCurrentUser();
		$params = array('username'	=> $user->get("username"),
        			 	'phone' 	=> $user->get("phone"),
                     	'address'   => $user->get("address"),
						);
		if($method == "POST"){
			if(isset($_POST['bid']))
				$bid = $_POST['bid'];
			if(isset($_POST['quantity']))
				$quantity = $_POST['quantity'];
			if(isset($_POST['cost']))
				$cost = $_POST['cost'];
			if(isset($_POST['username']))
				$phone = $_POST['username'];
			if(isset($_POST['phone']))
				$phone = $_POST['phone'];
			if(isset($_POST['address']))
				$address = $_POST['address'];
			$usermana = $this->registry->db->get('userManager');
			$usermana->bookDeal($user->get("uid"), $bid, $quantity, $cost, $phone, $address);
			header("Location: ../index");			
		}
		return $this->registry->template->getTemplate('accInfo', $params, 'Module'); 
	}
}

?>
